package yao.util.personal.ddl;



import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

@SuppressWarnings("deprecation")
public class AreaSqlCreator {
	private static final Log log = LogFactory.getLog(AreaSqlCreator.class);
	private static HttpClient httpClient;
	private static String province = "https://easybuy.jd.com//address/getProvinces.action";
	private static String city =     "https://easybuy.jd.com//address/getCitys.action?provinceId=%s";
	private static String area =     "https://easybuy.jd.com//address/getCountys.action?cityId=%s";
	private static String cookie = "thor=85A1B9683435608BED91A6102ABB54615AC9600B38A78FC5C031A52E968289E4997227888F169584FC23FA290DE587AEBBA0393FFA1D2A4790798D9A582729E1D7904270AF2D0C3DEFDAF1B0A02710BA0F00723E7ACD2049DFE86F6DFADCD17DD62D09929A5A8C37A0D25A60D4FF679534A1999090883708E2EEE2FFC5F379393B1DADD250CCD480C63CAD156A8F16098CECD2B9A7B6F315C52CDBE283762FE7";
	
	public static void main(String[] args) {
		httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(province);
		httpGet.addHeader(new BasicHeader("Cookie", cookie));
		try {
			HttpResponse response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				String result = EntityUtils.toString(entity);
				Map<String,String> map = sort(JSONObject.parse(result));
				//genProvinceSql(map);
				//genCitySql(map);
				genAreaSql(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings({ "all" })
	public static TreeMap<String,String> sort(Object object){
		TreeMap<String,String> treeMap = new TreeMap<String, String>(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				Integer io1 = Integer.parseInt(o1);
				Integer io2 = Integer.parseInt(o2);
				return io1.compareTo(io2);
			}
		});
		treeMap.putAll((Map)object);
		return treeMap;
	}
	
	public static void genProvinceSql(Map<String,String> map){
		String sql = "insert into test_province('id','name','active') values (%s,%s,%s)";
		for(Entry<String, String> entry : map.entrySet()){
			System.out.println(String.format(sql, "'"+entry.getKey()+"'", "'"+entry.getValue()+"'","'1'"));;
		}
		log.info("################################################");
	}
	
	public static void genCitySql(Map<String,String> map){
		String sql = "insert into test_city('id','province_id','name','active') values (%s,%s,%s,%s);";
		
		for(Entry<String, String> entry : map.entrySet()){
			HttpGet httpGet = new HttpGet(String.format(city, entry.getKey()));
			httpGet.addHeader(new BasicHeader("Cookie", cookie));
			try {
				HttpResponse response = httpClient.execute(httpGet);
				if (response.getStatusLine().getStatusCode() == 200) {
					HttpEntity entity = response.getEntity();
					String result = EntityUtils.toString(entity);
					Map<String,String> map_c = sort(JSONObject.parse(result));
					for(Entry<String, String> e1 : map_c.entrySet()){
						log.info(String.format(sql, "'"+e1.getKey()+"'", "'" + entry.getKey() + "'" , "'"+e1.getValue()+"'","'1'"));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void genAreaSql(Map<String,String> map){
		String sql = "insert into test_area('id','city_id','name','active') values (%s,%s,%s,%s);";
		for(Entry<String, String> entry : map.entrySet()){
			HttpGet httpGet = new HttpGet(String.format(city, entry.getKey()));
			httpGet.addHeader(new BasicHeader("Cookie", cookie));
			try {
				HttpResponse response = httpClient.execute(httpGet);
				if (response.getStatusLine().getStatusCode() == 200) {
					HttpEntity entity = response.getEntity();
					String result = EntityUtils.toString(entity);
					Map<String,String> map_c = sort(JSONObject.parse(result));
					for(Entry<String, String> e1 : map_c.entrySet()){
						HttpGet httpGet1 = new HttpGet(String.format(area, e1.getKey()));
						httpGet1.addHeader(new BasicHeader("Cookie", cookie));
						String result1 = null;
						try {
							HttpResponse response1 = httpClient.execute(httpGet1);
							if (response1.getStatusLine().getStatusCode() == 200) {
								HttpEntity entity1 = response1.getEntity();
								result1 = EntityUtils.toString(entity1);
								if(StringUtils.isEmpty(result1))	continue;
								Map<String,String> map_a = sort(JSONObject.parse(result1));
								for(Entry<String, String> e2 : map_a.entrySet()){
									log.info(String.format(sql, "'"+e2.getKey()+"'", "'" + e1.getKey() + "'" , "'"+e2.getValue()+"'","'1'"));;
								}
							}
						} catch (Exception e) {
							log.error("<<<<<<<<<<<<<<<<-->" + result1);
							log.error("<<<<<<<<<<<<<<<<-->" + httpGet1);
							e.printStackTrace();
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
